<?php
/**
 * $Id: view.html.php 11 2010-05-18 11:39:11Z webbochsant $
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 

//Import view-class
jimport( 'joomla.application.component.view');
 
/**
 * HTML View class for the VMCompetition Component
 *
 * @package    VMCompetition
 */
 
class VmcompetitionViewVmcompetition extends JView
{
    function display($tpl = null)
    {
    	global $mainframe;
   		$db 	  =& JFactory::getDBO();
		$document =& JFactory::getDocument();
		$pathway  =& $mainframe->getPathway();

		$comp_id = JRequest::getVar( 'id', 0, '', 'int' );

		$comp =& JTable::getInstance('vmcompetition', 'Table');
		$comp->load( $comp_id );

		// if id value is passed and poll not published then exit
		/*
		if ($poll->id > 0 && $poll->published != 1) {
			JError::raiseError( 403, JText::_('Access Forbidden') );
			return;
		}
		*/
        
        // Adds parameter handling
		$params = $mainframe->getParams();
		
		///Set page title information
		$menus	= &JSite::getMenu();
		$menu	= $menus->getActive();

		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				$params->set('page_title',	$comp->title);
			}
		} else {
			$params->set('page_title',	$comp->title);
		}
		$document->setTitle( $params->get( 'page_title' ) );

		//Set pathway information
		$pathway->addItem($comp->title, '');
		
		//Get Competitions
		/*
		$query = 'SELECT a.*, b.product_name AS prodname, b.product_thumb_image AS prodimage'
				. ' FROM #__vm_competition AS a'
				. ' INNER JOIN #__vm_product AS b ON b.product_id = a.pid'
				. ' ORDER BY a.publish_up DESC';
		*/
				
		//Get current time and date		
		$date =& JFactory::getDate();
		//Get all current competitions
		$query = 'SELECT a.*, b.product_name AS prodname, b.product_thumb_image AS prodimage, b.product_full_image AS fullimage'
				. ' FROM #__vm_competition AS a'
				. ' INNER JOIN #__vm_product AS b ON b.product_id = a.pid'
				. ' WHERE (a.publish_up < '.$db->Quote($date->toMySQL()).') AND (a.publish_down > '.$db->Quote($date->toMySQL()).')'
				. ' ORDER BY a.publish_up DESC';
				
			$db->setQuery( $query );
			$competitions = $db->loadObject();
			
			$this->assignRef('competitions',	$competitions);
			
		//Get Winners
		
		$querywinners = 'SELECT w . * , cmp.title AS comp_name, cnt.name AS cont_name, cnt.linkurl AS linkurl'
        . ' FROM `#__vm_competition_winners` AS w'
        . ' INNER JOIN `#__vm_competition` AS cmp ON cmp.id = w.`comp_id`'
        . ' LEFT JOIN `#__vm_competition_contestants` AS cnt ON cnt.cid = w.cont_id'
        . ' ORDER BY w.wid DESC'
        ;
		$db->setQuery( $querywinners );
		$winners = $db->loadObjectList();
		
		$this->assignRef('params',	$params);
		
		$this->assignRef('winners',	$winners);
		
		//$name = JRequest::getVar( 'name');
		//$this->assignRef('namnet', JRequest::getVar( 'namnet'));
		
        parent::display($tpl);
    }
}

?>